 source 'https://github.com/CocoaPods/Specs.git'
#source 'https://cdn.cocoapods.org/'

source 'https://github.com/TuyaInc/TuyaPublicSpecs.git'

source 'https://github.com/tuya/tuya-pod-specs.git'

ENV['SDKROOT'] = `xcrun --sdk macosx --show-sdk-path`.strip
ENV['DEVELOPER_DIR'] = `xcode-select -p`.strip
ENV['CC'] = '/usr/bin/cc'


# Uncomment this line to define a global platform for your project
 platform :ios, '15.6'

# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'true'

# 优化下载配置，特别是针对 OpenCV 等大型框架
install! 'cocoapods', :deterministic_uuids => false

# 👇 如果 tuya_home_sdk_flutter 内部有 React 依赖
#pod 'React', :path => '.symlinks/plugins/tuya_home_sdk_flutter/ios/Assets'

project 'Runner', {
  'Debug' => :debug,
  'Profile' => :release,
  'Release' => :release,
}

def flutter_root
  generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'Generated.xcconfig'), __FILE__)
  unless File.exist?(generated_xcode_build_settings_path)
    raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure flutter pub get is executed first"
  end

  File.foreach(generated_xcode_build_settings_path) do |line|
    matches = line.match(/FLUTTER_ROOT\=(.*)/)
    return matches[1].strip if matches
  end
  raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Generated.xcconfig, then run flutter pub get"
end

require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root)

flutter_ios_podfile_setup



target 'Runner' do
  use_frameworks!
  #pod 'glog', :path => './fix_glog.podspec'
  pod 'ThingSmartCryption', :path => './ios_core_sdk'

  # 👇 添加这行解决 prerelease 版本找不到问题
  pod 'ThingNetworkAreaInterceptorAPI', '1.1.11-feature-anonymize-4.4.0.1'
  # 显式指定 ThingBluetoothInterface 版本以解决依赖冲突
  pod 'ThingBluetoothInterface', '1.20.1'
  # 显式指定 dsBridge 版本以解决依赖冲突
  pod 'dsBridge', '3.0.202.1'
  pod 'ThingSmartLogger'
  
  ### RN面板调试
  # pod 'ThingRNPanelTool'
  # pod 'React', :podspec => 'https://images.tuyacn.com/rnpanel/React.podspec.json' # 调试包链接https://images.tuyacn.com/rnpanel/React.podspec.json
#   pod 'ThingSmartHomeKit' ,'~> 6.2.0'


#   pod 'ThingSmartThemeManagerBizBundle','~> 6.2.0'
#   pod 'ThingSmartPanelBizBundle','~> 6.2.0'


# # 添加设备详情 UI 业务包
# pod 'ThingSmartDeviceDetailBizBundle'
# # 涂鸦消息中心UI 业务包
# pod 'ThingSmartMessageBizBundle'
  
#  pod 'WechatOpenSDK-XCFramework', '~> 2.0.4'
  flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
  target 'RunnerTests' do
    inherit! :search_paths
  end
end
post_install do |installer|
  installer.pods_project.targets.each do |target|
    flutter_additional_ios_build_settings(target)
    target.build_configurations.each do |config|
      config.build_settings["IPHONEOS_DEPLOYMENT_TARGET"] = "15.6"
      config.build_settings["EXCLUDED_ARCHS[sdk=iphonesimulator*]"] = "arm64"
      config.build_settings["DEVELOPMENT_TEAM"] = "3BSH39KMQX"

      # 开启位置权限 
      config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
        '$(inherited)'
      ]
      definitions = config.build_settings['GCC_PREPROCESSOR_DEFINITIONS']
      unless definitions.include?('PERMISSION_CAMERA=1')
        definitions << 'PERMISSION_CAMERA=1'
      end
      unless definitions.include?('PERMISSION_LOCATION=1')
        definitions << 'PERMISSION_LOCATION=1'
      end
      unless definitions.include?('PERMISSION_LOCATION_WHEN_IN_USE=1')
        definitions << 'PERMISSION_LOCATION_WHEN_IN_USE=1'
      end
    end
  end

  # ✅ glog missing 修复逻辑放在 post_install 块内，但在 targets 处理之外
  glog_path = Dir.glob(File.expand_path("~/Library/Caches/CocoaPods/Pods/Release/glog/*")).first
  if glog_path
    missing_file = File.join(glog_path, "missing")
    if !File.exist?(missing_file) || File.read(missing_file).include?("--is-lightweight")
      puts "🔧 Fixing broken glog/missing script..."
      system("curl -o #{missing_file} https://raw.githubusercontent.com/autotools-mirror/automake/master/lib/missing")
      system("chmod +x #{missing_file}")
    end
  else
    puts "⚠️ glog not found in CocoaPods cache, skipping fix."
  end
end
